package com.leha.forum.lehare.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leha.forum.lehare.entity.Carousel;
import com.leha.forum.lehare.response.Result;
import com.leha.forum.lehare.service.CarouselService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author lovelycat
 * @version 1.0
 * @since 2025-04-16 15:34
 */
@RestController
@RequestMapping("/carousel")
public class CarouselController {
    @Resource
    private CarouselService carouselService;

    @GetMapping("/all")
    public Result<?> getAllCarousels() {
        // 获取所有有效的轮播图并按优先级从高到低排序
        return Result.success(
                "",
                carouselService.list(
                        new QueryWrapper<Carousel>()
                                .eq("is_active", true)
                                .orderByDesc("priority")
                )
        );
    }

    @PostMapping("/save")
    public Result<?> saveOrUpdateCarousel(@RequestBody Carousel carousel) {
        boolean success = false;
        if (carousel.getCarouselId() != null && carousel.getCarouselId() > 0) {
            success = carouselService.updateById(carousel);
        } else {
            success = carouselService.save(carousel);
        }

        if (success) {
            return Result.success("添加成功", null);
        } else {
            return Result.failed(401, "添加失败");
        }
    }
}
